library(snowfall)
library(devtools)
library(CoxBoost)
library(fastAdaboost)
library(Mime1)
library(IOBR)
library(pheatmap)
library(dplyr)
library(immunedeconv)
library(Cairo)
library(xgboost)

try({
  setwd('./mimeNewPackage/') 
})


load("./Example.cohort.Rdata")
load("./genelist.Rdata")

try({
  load('res.rdata')
})


if (!exists("res")) {
  message("res 不存在 执行计算")
  res <- ML.Dev.Prog.Sig(train_data = list_train_vali_Data$Dataset1,
                         list_train_vali_Data = list_train_vali_Data,
                         unicox.filter.for.candi = T,
                         unicox_p_cutoff = 0.05,
                         candidate_genes = genelist,
                         mode = 'all',nodesize =5,seed = 5201314 )
}else{
  message("res 存在 引用算好的")
}


custom_colors <- c("#4195C1", "#ecf0f1", "#CB5746","#3fa0c0", "#d5d9e5")
hm <- cindex_dis_all(
  res,
  color = custom_colors,       # 传递自定义颜色设置
  validate_set = "Dataset1",   # 指定测试集的名称
  # order = names(list_train_vali_Data),
  width = 0.35
)
cellwidth = 1
cellheight = 0.5
Cindex.res <- res[["Cindex.res"]]
pdf(file.path( "heatmap1.pdf"), width = cellwidth * 4 + 5, height = cellheight * nrow(Cindex.res) * 0.1)
print(hm)
dev.off()
try({
  png(file="heatmap1.png", width=900, height=1200)
  hm
  dev.off()
})


riskscore <- res[["riskscore"]]
riskscore_1 <- riskscore[["StepCox[forward] + GBM"]]
riskscore_1_TCGA <- riskscore_1[["Dataset1"]]
riskscore_1_CGGA <- riskscore_1[["Dataset2"]]
write.table(riskscore_1_TCGA,file="TCGAriskcore.txt",sep = "\t",row.names = T,col.names = NA,quote = F)
write.table(riskscore_1_CGGA,file="CGGAriskscore.txt",sep = "\t",row.names = T,col.names = NA,quote = F)

try({
  res.feature.all <- read.table('res.feature.all.txt',sep="\t",header=T,check.names=F)
})

if (!exists("res.feature.all")) {
  message("res.feature.all 不存在 执行计算")
  res.feature.all <- ML.Corefeature.Prog.Screen(InputMatrix = list_train_vali_Data$Dataset1,
                                                candidate_genes = genelist,
                                                mode = "all",nodesize =5,seed = 5201314 )
}else{
  message("res.feature.all 存在")
}




write.table(res.feature.all,"所有算法筛选对象.txt", col.names = T, row.names = F, sep = "\t", quote = F)

write.table(res.feature.all,"res.feature.all.txt", col.names = T, row.names = F, sep = "\t", quote = F)

# core_feature_select(res.feature.all) ##空的
##实现上一步骤 Upset图




# 设置颜色和比例
col <- c("#E18727", "#B09C85", "#ADB6B6", "#B09C85")
mb.ratio <- c(0.6, 0.4)

# 创建核心特征列表
core_feature_list <- list()
for (i in unique(res.feature.all$method)) {
  core_feature_list[[i]] <- res.feature.all[res.feature.all$method == i, "selected.fea"]
}

# 创建 UpSet 图
p1 <- upset(
  fromList(core_feature_list),
  sets = names(core_feature_list),
  order.by = "freq",
  nintersects = NA,
  mb.ratio = mb.ratio,
  keep.order = TRUE,
  mainbar.y.label = "Shared gene number",
  sets.x.label = "Total gene number",
  point.size = 2,
  line.size = 1,
  sets.bar.color = col[1],
  main.bar.color = col[2],
  matrix.color = col[3],
  shade.color = col[4]
)

# 显示 UpSet 图
pdf("core_feature_upset_plot.pdf", width = 10, height = 6)
print(p1)
dev.off()

# CairoTIFF(file="core_feature_upset_plot.tiff", width=150, height=150,units="in",dpi=120)
CairoTIFF(file="core_feature_upset_plot.tiff", width=cellwidth * 4 + 5, height=cellheight * nrow(Cindex.res) * 0.1,units="in",dpi=120)
p1
dev.off()
